package com.smedi.ismedi.inmail.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement.AttachmentDetail;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement.AttachmentDetailInfo;
import com.smedi.ismedi.inmail.core.domain.repository.AttachmentDetailRepository;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.AnnouncementAttachmentRelationshipEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.AnnouncementAttachmentRelationshipMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 主体院关系实现类
 *
 * @author niuyanling
 * @date 2021/08/05
 */
@Repository
@AllArgsConstructor
public class    AttachmentDetailRepositoryImpl extends ServiceImpl<AnnouncementAttachmentRelationshipMapper, AnnouncementAttachmentRelationshipEntity> implements AttachmentDetailRepository, IService<AnnouncementAttachmentRelationshipEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public AttachmentDetail doInsert(AttachmentDetail attachmentDetail) {
        AnnouncementAttachmentRelationshipEntity announcementAttachmentRelationshipEntity = dozerUtil.convertor(attachmentDetail, AnnouncementAttachmentRelationshipEntity.class);

        this.getBaseMapper().insert(announcementAttachmentRelationshipEntity);

        return this.dozerUtil.convertor(announcementAttachmentRelationshipEntity, AttachmentDetail.class);
    }

    @Override
    public AttachmentDetailInfo doSelectByAnnouncementId(Long announcementId) {
        List<String> attachmentKeyList = new ArrayList<>();
        List<String> attachmentNameList = new ArrayList<>();
        List<String> attachmentExtensionList = new ArrayList<>();
        AttachmentDetailInfo attachmentDetailInfo = new AttachmentDetailInfo();
        QueryWrapper<AnnouncementAttachmentRelationshipEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("announcement_id", announcementId);
        List<AnnouncementAttachmentRelationshipEntity> announcementAttachmentRelationshipEntityList = this.getBaseMapper().selectList(queryWrapper);
        if (!announcementAttachmentRelationshipEntityList.isEmpty()) {
            for (AnnouncementAttachmentRelationshipEntity announcementAttachmentRelationshipEntity : announcementAttachmentRelationshipEntityList) {
                attachmentKeyList.add(announcementAttachmentRelationshipEntity.getAttachmentKey());
            }
        }
        attachmentDetailInfo.setAttachmentKeyList(attachmentKeyList)
                .setAttachmentNameList(attachmentNameList)
                .setAttachmentExtensionList(attachmentExtensionList);

        return attachmentDetailInfo;
    }
}
